﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Subsonic.Data;
using Subsonic.Resources;
using Subsonic.Controls;

namespace Subsonic.Pages
{
    public partial class ChoicePage : PhoneApplicationPage
    {
        public ChoicePage()
        {
            InitializeComponent();
        }

        Choice Choice { get; set; }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string choiceType;
            if (NavigationContext.QueryString.TryGetValue("choiceType", out choiceType))
            {
                Choice = Choice.GetChoice(choiceType);
                if (Choice != null)
                {
                    this.PageTitle.Text = Choice.Title.StaticResource();
                    foreach (var v in Choice.Values)
                    {
                        RadioButton button = null;
                        if (v == Choice.CurrentValue)
                        {
                            button = new RadioButton() { Content = v.StaticResource(), GroupName = Choice.Title.StaticResource(), IsChecked = true };
                        }
                        else
                        {
                            button = new RadioButton() { Content = v.StaticResource(), GroupName = Choice.Title.StaticResource() };
                        }
                        Choices.Children.Add(button);
                        button.Checked +=new RoutedEventHandler(button_Checked);
                        button.Click += new RoutedEventHandler(button_Click);
                    }
                }
            }
        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        void button_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton button = (RadioButton)sender;
            Choice.SetNewValue((string)button.Content);
        }
    }
}